var createError = require('http-errors');
var express = require('express');

var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
//引入管理员注册登录路由
var adminsRouter = require('./routes/admins');
//引入用户注册登录路由
var usersRouter = require('./routes/users');
// 引入自提点路由
var addressRouter=require('./routes/address');


// 引入父类型路由
var typesRouter = require('./routes/types');
// 引入子类型路由
var labelsRouter = require('./routes/labels');
// 引入商品路由
var commsRouter = require('./routes/comms');
// 引入订单路由
var indentRouter= require('./routes/indent');
// 引入令牌验证
const { jwtAuth } = require("tools-factory-tang/jwt");
// 引入初始化路由
var indexRouter = require('./routes/index');
// 引入秒杀模块路由
var secondarilyRouter=require("./routes/secondKill")
// 引入管理员列表路由
var adminsDataRouter = require('./routes/adminsData');
// 引入自提点列表路由
var addressDataRouter = require('./routes/address');

var shoppingRouter = require('./routes/shopping');

// 引入db 只需要引入，建立连接，保证能在项目开始数据库就与服务器连接
require("./db/index")

var app = express();




// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// 引⼊ swagger
require("./swagger")(app);

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

// 路由挂载到路径
app.use('/comms', commsRouter);
// 路由挂载到路径
app.use('/admins', adminsRouter);
app.use('/adminsData',adminsDataRouter);
app.use('/address',addressDataRouter);
// 路由挂载到路径
app.use('/labels', labelsRouter);
app.use(jwtAuth);
// 类型路由挂载到路径
app.use('/types', typesRouter);
// 路由挂载到路径
app.use('/users', usersRouter);
// 自提点挂载到路径
app.use('/address',addressRouter);
// 路由挂载到路径
app.use('/', indexRouter);
// 秒杀路由挂载路径
app.use('/secondKill',secondarilyRouter);

app.use('/indent', indentRouter);
app.use('/shopping', shoppingRouter);


// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
